package com.lanhe.game.advertising.service.impl;

import com.lanhe.common.constant.HttpStatus;
import com.lanhe.common.core.redis.RedisCache;
import com.lanhe.common.enums.AdSetType;
import com.lanhe.common.exception.ServiceException;
import com.lanhe.common.utils.DateUtils;
import com.lanhe.game.adSetEarnings.domain.LhAdsetEarnings;
import com.lanhe.game.adSetEarnings.mapper.LhAdsetEarningsMapper;
import com.lanhe.game.advertising.domain.LhAdsetAdvertisingRec;
import com.lanhe.game.advertising.domain.dto.AdvertisingCallbackDTO;
import com.lanhe.game.advertising.mapper.LhAdsetAdvertisingRecMapper;
import com.lanhe.game.advertising.service.ILhAdsetAdvertisingRecService;
import com.lanhe.game.sysAppUser.domain.SysAppUser;
import com.lanhe.game.sysAppUser.mapper.SysAppUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 用户广告完成统计Service业务层处理
 * 
 * @author tianliang
 * @date 2024-02-05
 */
@Service
public class LhAdsetAdvertisingRecServiceImpl implements ILhAdsetAdvertisingRecService 
{
    @Autowired
    private LhAdsetAdvertisingRecMapper lhAdsetAdvertisingRecMapper;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private LhAdsetEarningsMapper lhAdsetEarningsMapper;

    @Autowired
    private SysAppUserMapper sysAppUserMapper;

    /**
     * 查询用户广告完成统计
     * 
     * @param id 用户广告完成统计主键
     * @return 用户广告完成统计
     */
    @Override
    public LhAdsetAdvertisingRec selectLhAdsetAdvertisingRecById(Long id)
    {
        return lhAdsetAdvertisingRecMapper.selectLhAdsetAdvertisingRecById(id);
    }

    /**
     * 查询用户广告完成统计列表
     * 
     * @param lhAdsetAdvertisingRec 用户广告完成统计
     * @return 用户广告完成统计
     */
    @Override
    public List<LhAdsetAdvertisingRec> selectLhAdsetAdvertisingRecList(LhAdsetAdvertisingRec lhAdsetAdvertisingRec)
    {
        return lhAdsetAdvertisingRecMapper.selectLhAdsetAdvertisingRecList(lhAdsetAdvertisingRec);
    }

    /**
     * 新增用户广告完成统计
     * 
     * @param lhAdsetAdvertisingRec 用户广告完成统计
     * @return 结果
     */
    @Override
    public int insertLhAdsetAdvertisingRec(LhAdsetAdvertisingRec lhAdsetAdvertisingRec)
    {
        lhAdsetAdvertisingRec.setCreateTime(DateUtils.getNowDate());
        return lhAdsetAdvertisingRecMapper.insertLhAdsetAdvertisingRec(lhAdsetAdvertisingRec);
    }

    /**
     * 修改用户广告完成统计
     * 
     * @param lhAdsetAdvertisingRec 用户广告完成统计
     * @return 结果
     */
    @Override
    public int updateLhAdsetAdvertisingRec(LhAdsetAdvertisingRec lhAdsetAdvertisingRec)
    {
        lhAdsetAdvertisingRec.setUpdateTime(DateUtils.getNowDate());
        return lhAdsetAdvertisingRecMapper.updateLhAdsetAdvertisingRec(lhAdsetAdvertisingRec);
    }

    /**
     * 批量删除用户广告完成统计
     * 
     * @param ids 需要删除的用户广告完成统计主键
     * @return 结果
     */
    @Override
    public int deleteLhAdsetAdvertisingRecByIds(Long[] ids)
    {
        return lhAdsetAdvertisingRecMapper.deleteLhAdsetAdvertisingRecByIds(ids);
    }

    /**
     * 删除用户广告完成统计信息
     * 
     * @param id 用户广告完成统计主键
     * @return 结果
     */
    @Override
    public int deleteLhAdsetAdvertisingRecById(Long id)
    {
        return lhAdsetAdvertisingRecMapper.deleteLhAdsetAdvertisingRecById(id);
    }

    /**
     * 用户每完成一次广告任务调用一次
     * @param dto
     */
    @Override
    public void adsetallback(AdvertisingCallbackDTO dto) {

        SysAppUser sysAppUser = sysAppUserMapper.selectSysAppUserByOpenId(dto.getOpenId());
        if (Objects.isNull(sysAppUser)){
            throw new ServiceException("用户不存在", HttpStatus.ERROR);
        }
        String userKey = dto.getAdType()+":"+dto.getOpenId();
        // 根据当天的年月日作为大key
        Date date = new Date();
        String yyyyMMdd = DateUtils.parseDateToStr("yyyyMMdd", date);

        // 获取当下的用户内容
        Map<String, Integer> cacheMap = redisCache.getCacheMap(yyyyMMdd);
        if (cacheMap == null){
            System.err.println("今日创建的第一个");
            HashMap<String, Integer> map = new HashMap<>();
            map.put(userKey,1);
            redisCache.setCacheMap(yyyyMMdd,map);
        }else {
            System.err.println("redis里面有数据,需要取出来进行增加");
            if (cacheMap.containsKey(userKey)){
                // 说明里面是存在key的
                Integer count = (Integer)cacheMap.get(userKey);
                count +=1;
                cacheMap.put(userKey,count);
            }else {
                System.err.println("大key有,没有小key");
                cacheMap.put(userKey,1);
            }
            redisCache.setCacheMap(yyyyMMdd,cacheMap);
        }
        System.err.println("redis的内容 "+cacheMap);
    }
    /**
     * 将昨天的数据刷入到数据库中
     */
    @Override
    public void adsetDataList() {
        //获取昨日的日期
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.DATE,-1);
        String yyyyMMdd = DateUtils.parseDateToStr("yyyyMMdd",instance.getTime());
        System.err.println("yyyyMMdd "+yyyyMMdd);
        Map<String, Integer> cacheMap = redisCache.getCacheMap(yyyyMMdd);
        if (cacheMap != null){
            System.err.println(" 今日map里面的数据 "+cacheMap);
            Iterator<String> iterator = cacheMap.keySet().iterator();
            int m_videoCount = 0;   // 记录激励视频的点击总数量
            int tp_advertisingCount = 0;   // 记录插屏广告的总数量
            int banner_advertisingCount = 0; // 记录banner广告的总数量

            while (iterator.hasNext()){
                /**
                 * 将数据刷入到统计表中
                 */
                String key = iterator.next();
                String[] split = key.split(":");
                if (split.length == 2){
                    // 这是用户的广告类型  1,是激励视频 2,插屏广告 3,banner广告
                    String adType = split[0];
                    String openId = split[1];
                    Integer count = cacheMap.get(key);
                    Long advertisingId = redisCache.increment("advertisingId");   // 使用redis的自增做id
                    LhAdsetAdvertisingRec lhAdsetAdvertisingRec = new LhAdsetAdvertisingRec();
                    lhAdsetAdvertisingRec.setId(advertisingId);
                    lhAdsetAdvertisingRec.setAdType(adType);
                    lhAdsetAdvertisingRec.setOpenId(openId);
                    lhAdsetAdvertisingRec.setCount(count);
                    lhAdsetAdvertisingRec.setCreateTime(new Date());
                    System.err.println("每条数据的展示 "+lhAdsetAdvertisingRec);
                    lhAdsetAdvertisingRecMapper.insertLhAdsetAdvertisingRec(lhAdsetAdvertisingRec);
                }
                /**
                 * 将数据刷入到收益分配中
                 */
                int count = cacheMap.get(key);
                System.err.println("count "+count);
                System.err.println("split[0]"+split[0]);

                if (AdSetType.M_video.getCode().equals(split[0])){
                    // 这是 激励视频
                    System.err.println("激励视频");
                    m_videoCount += count;
                }
                if (AdSetType.Tp_advertising.getCode().equals(split[0])){
                    System.err.println("这是插屏广告");
                    // 这是插屏广告
                    tp_advertisingCount += count;
                }
                if (AdSetType.Banner_advertising.getCode().equals(split[0])){
                    System.err.println("这是banner广告");
                    // 这是banner广告
                    banner_advertisingCount += count;
                }
            }

            Long adsetEarningsId = redisCache.increment("AdsetEarningsId"); //  用redis创建主键id
            LhAdsetEarnings lhAdsetEarnings = new LhAdsetEarnings();
            lhAdsetEarnings.setAdId(adsetEarningsId);
            lhAdsetEarnings.setmVideoCount((long)m_videoCount);
            lhAdsetEarnings.setTpAdvertisingCount((long)tp_advertisingCount);
            lhAdsetEarnings.setBannerAdvertisingCount((long)banner_advertisingCount);
            lhAdsetEarnings.setState(0);
            lhAdsetEarnings.setCreateTime(new Date());
            System.err.println("添加的数据是 "+lhAdsetEarnings);
            lhAdsetEarningsMapper.insertLhAdsetEarnings(lhAdsetEarnings);
        }

    }
}
